package com.fyzk.activity.lottery.jc.football;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.adapter.JCZQZhiFuBaseAdapter;
import com.fyzk.interfaces.OnFootballPayTeamDeleteListener;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.utils.LogUtils;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.List;

public class JCZQ_HG_ZhiFuAdapter extends JCZQZhiFuBaseAdapter {

    private static final String TAG = "JC_ZhiFuAdapter";

    private boolean isdanguan;
    private Handler handler;
    private int zhuShu;

    private int color_hei;
    private int color_zong;
    private OnFootballPayTeamDeleteListener listener;
    private int danCount = 0;
    private String chuanStr = JC_GuoGuanDialog.result;
    private int color_hui;
    private Context context;

    public JCZQ_HG_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler handler,
                                OnFootballPayTeamDeleteListener listener) {
        this.isdanguan = type;
        this.infors = getInfor(infors);
        this.context = context;
        this.handler = handler;
        color_hei = context.getResources().getColor(R.color.black);
        color_zong = context.getResources().getColor(R.color.zc_bf_left);
        this.listener = listener;
        color_hui = context.getResources().getColor(R.color.gray);
        refreshDan();
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public Object getItem(int position) {
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.jz_zhifu_hg_item, null);
            holder = new ViewHolder();
            holder.zhu = convertView.findViewById(R.id.jc_zhifu_list_item_zhu);
            holder.delete = convertView.findViewById(R.id.jc_zhifu_list_item_delete);
            holder.team_home = convertView.findViewById(R.id.jc_zhifu_list_item_tv_sheng1);
            holder.vs_tag = convertView.findViewById(R.id.jc_zhifu_list_item_tv_ping1);
            holder.team_away = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu1);
            holder.zhanKai = convertView.findViewById(R.id.jczq_list_right_hh_zhankai);
            holder.week_num = convertView.findViewById(R.id.week_num);
            holder.sheng2 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_sheng2);
            holder.ping2 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_ping2);
            holder.fu2 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu2);
            holder.sheng2_r = convertView.findViewById(R.id.jc_zhifu_list_item_tv_sheng2_r);
            holder.ping2_r = convertView.findViewById(R.id.jc_zhifu_list_item_tv_ping2_r);
            holder.fu2_r = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu2_r);
            holder.txt_rf = convertView.findViewById(R.id.txt_rf);
            holder.dan = convertView.findViewById(R.id.jc_zhifu_list_item_dan);
            holder.txt_stop1 = convertView.findViewById(R.id.jczq_list_right_hh_zf_stop1);
            holder.txt_stop2 = convertView.findViewById(R.id.jczq_list_right_hh_zf_stop2);
            holder.ll_rspf = convertView.findViewById(R.id.hh_zf_rspf);
            holder.ll_spf = convertView.findViewById(R.id.hh_zf_spf);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCInfo infor = infors.get(position);
        if (isdanguan) {
            holder.dan.setVisibility(View.GONE);
        } else {
            holder.dan.setVisibility(View.VISIBLE);
            holder.dan.setChecked(infor.isDan());
            if (infor.isDaneble()) {
                holder.dan.setEnabled(true);
                holder.dan.setTag(R.string.jc_info, infor);
                holder.dan.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                        infor.setDan(!infor.isDan());
                        refreshDan();
                    }
                });
            } else {
                holder.dan.setEnabled(false);
            }
        }
        holder.zhu.setVisibility(View.VISIBLE);
        if (infor.getStaticSaleSpfWrqStatus() == 1) {//胜平负过关停售
            holder.ll_spf.setVisibility(View.GONE);
            holder.txt_stop1.setVisibility(View.VISIBLE);
        } else {
            holder.ll_spf.setVisibility(View.VISIBLE);
            holder.txt_stop1.setVisibility(View.GONE);
        }
        if (infor.getStaticSaleSpfStatus() == 1) {//让球胜平负过关停售
            holder.ll_rspf.setVisibility(View.GONE);
            holder.txt_stop2.setVisibility(View.VISIBLE);
        } else {
            holder.ll_rspf.setVisibility(View.VISIBLE);
            holder.txt_stop2.setVisibility(View.GONE);
        }
        holder.team_home.setText(infor.getHomeTeam());
        holder.team_away.setText(infor.getAwayTeam());
        holder.vs_tag.setText("vs");
        holder.txt_rf.setText("让\n" + infor.getHandicap());
        if (infor.getHandicap().contains("+")) {
            holder.txt_rf.setBackgroundColor(context.getResources().getColor(R.color.jczq_hh_l_bg));
        } else {
            holder.txt_rf.setBackgroundColor(context.getResources().getColor(R.color.jczq_hh_r_bg));
        }
        if (infor.getHg_more() > 0) {
            holder.zhanKai.setBackgroundDrawable(context.getResources().getDrawable(R.drawable
                    .button_red_sendredpacket));
            holder.zhanKai.setText(infor.getHg_more() + "");
            holder.zhanKai.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            holder.zhanKai.setText("更多\n玩法");
            holder.zhanKai.setTextColor(context.getResources().getColor(R.color.black));
            holder.zhanKai.setBackgroundColor(Color.WHITE);
        }
        holder.zhanKai.setTag(infor);
        holder.zhanKai.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.open((JCInfo) v.getTag());
            }
        });
        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor.getMatchNum()));
        holder.sheng2.setOnCheckedChangeListener(null);
        holder.fu2.setOnCheckedChangeListener(null);
        holder.ping2.setOnCheckedChangeListener(null);
        holder.sheng2_r.setOnCheckedChangeListener(null);
        holder.fu2_r.setOnCheckedChangeListener(null);
        holder.ping2_r.setOnCheckedChangeListener(null);
        JCInfo.SPInfo sp_s = infor.btnHGPojo.get(infor.VS_3);
        JCInfo.SPInfo sp_p = infor.btnHGPojo.get(infor.VS_1);
        JCInfo.SPInfo sp_f = infor.btnHGPojo.get(infor.VS_0);
        JCInfo.SPInfo sp_rs = infor.btnHGPojo.get(infor.LETVS_3);
        JCInfo.SPInfo sp_rp = infor.btnHGPojo.get(infor.LETVS_1);
        JCInfo.SPInfo sp_rf = infor.btnHGPojo.get(infor.LETVS_0);
        holder.sheng2.setText("胜" + sp_s.spValue);
        holder.ping2.setText("平" + sp_p.spValue);
        holder.fu2.setText("负" + sp_f.spValue);
        holder.sheng2_r.setText("胜" + sp_rs.spValue);
        holder.ping2_r.setText("平" + sp_rp.spValue);
        holder.fu2_r.setText("负" + sp_rf.spValue);
        holder.sheng2.setChecked(sp_s.isSel);
        holder.ping2.setChecked(sp_p.isSel);
        holder.fu2.setChecked(sp_f.isSel);
        holder.sheng2_r.setChecked(sp_rs.isSel);
        holder.ping2_r.setChecked(sp_rp.isSel);
        holder.fu2_r.setChecked(sp_rf.isSel);
        holder.sheng2.setTag(sp_s);
        holder.sheng2.setTag(R.string.jc_info, infor);
        holder.sheng2.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.ping2.setTag(sp_p);
        holder.ping2.setTag(R.string.jc_info, infor);
        holder.ping2.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.fu2.setTag(sp_f);
        holder.fu2.setTag(R.string.jc_info, infor);
        holder.fu2.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.sheng2_r.setTag(sp_rs);
        holder.sheng2_r.setTag(R.string.jc_info, infor);
        holder.sheng2_r.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.ping2_r.setTag(sp_rp);
        holder.ping2_r.setTag(R.string.jc_info, infor);
        holder.ping2_r.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.fu2_r.setTag(sp_rf);
        holder.fu2_r.setTag(R.string.jc_info, infor);
        holder.fu2_r.setOnCheckedChangeListener(onCheckedChangeListener);
        holder.delete.setTag(R.string.jc_info, infor);
        holder.delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                if (listener != null) {
                    listener.onDelete(infor);
                }
                JCBasePayActivity.infos.remove(infor);
                setInfors(JCBasePayActivity.infos, isdanguan);
            }
        });
        jiSuanZhuShu();
        return convertView;
    }


    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton
            .OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            JCInfo.SPInfo spInfo = (JCInfo.SPInfo) buttonView.getTag();
            JCInfo infor = (JCInfo) buttonView.getTag(R.string.jc_info);
            spInfo.isSel = isChecked;
            int clickNum = infor.getOnclikNum() ;
            if (isChecked) {
                infor.setOnclikNum(clickNum + 1);
            } else {
                infor.setOnclikNum(clickNum - 1);
            }
            if (infor.getOnclikNum() == 0) {
                if (listener != null) {
                    listener.onDelete(infor);
                }
            }
            refreshDan();
        }
    };

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCInfo> getInfor(List<JCInfo> infors) {
        ArrayList<JCInfo> list = new ArrayList<JCInfo>();
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() > 0) {
                list.add(info);
            }
        }
        JCBasePayActivity.infos = list;
        return list;
    }

    /**
     * 计算注数（待修改） ///苗强 ---暂不支持 多串过关  只支持 自由过关
     */
    public void jiSuanZhuShu() {

        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();

        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() > 0) {
                changCi++;
                dannums.add(info.isDan());// 添加胆拖
                clicknums.add(info.getOnclikNum());
            }
            if (info.isDan()) {
                wanFa = 2;
            }

        }

        List<Integer> chuans = new ArrayList<Integer>();

        if (isdanguan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            String chuan = JC_GuoGuanDialog.result;
            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }

                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);

                String betcode = JZBetContentUtil.getContentHG(infors, isdanguan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isdanguan, dannums);

            }

        }
        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }

    public List<JCInfo> getInfors() {
        return infors;
    }

    public void setInfors(List<JCInfo> infors, boolean isDanguan) {
        this.isdanguan = isDanguan;
        this.infors = infors;
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        LinearLayout zhu;
        LinearLayout delete;
        TextView team_home;
        TextView week_num;
        TextView vs_tag;
        TextView team_away;
        TextView txt_rf;
        TextView zhanKai;
        CheckBox sheng2;
        CheckBox ping2;
        CheckBox fu2;
        CheckBox sheng2_r;
        CheckBox ping2_r;
        CheckBox fu2_r;
        CheckBox dan;
        LinearLayout ll_spf;
        LinearLayout ll_rspf;
        TextView txt_stop1;
        TextView txt_stop2;

    }

}
